/*
 * Proto Streamer
 * Copyright (C) 2009 John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package p.s;

/**
 * @author jdp
 */
public final class Protocol
    extends Object
{
    public final static Protocol HTTP10 = new Protocol("HTTP/1.0");
    public final static Protocol HTTP11 = new Protocol("HTTP/1.1");

    public final static Protocol Instance(String string){
        if (string.startsWith("HTTP/1")){
            switch (string.charAt(7)){
            case '0':
                return Protocol.HTTP10;
            case '1':
                return Protocol.HTTP11;
            }
        }
        return new Protocol(string);
    }


    public final String string, scheme, version;

    public final int versionMajor, versionMinor;


    private Protocol(String string){
        super();
        int idx = string.indexOf('/');
        if (0 < idx){
            this.string = string;
            this.scheme = string.substring(0,idx).toLowerCase();
            this.version = string.substring(idx+1);
            idx = this.version.indexOf('.');
            if (0 < idx){
                this.versionMajor = Integer.parseInt(this.version.substring(0,idx));
                this.versionMinor = Integer.parseInt(this.version.substring(idx+1));
            }
            else
                throw new IllegalArgumentException(string);
        }
        else
            throw new IllegalArgumentException(string);
    }


    public String getScheme(){
        return this.scheme;
    }
    public String getVersion(){
        return this.version;
    }
    public int getVersionMajor(){
        return this.versionMajor;
    }
    public int getVersionMinor(){
        return this.versionMinor;
    }
    public boolean isHTTP10(){
        return (Protocol.HTTP10 == this);
    }
    public boolean isHTTP11(){
        return (Protocol.HTTP11 == this);
    }
    public boolean isDefaultPort(Server server){
        return (80 == server.getPort());
    }
    public boolean isNotDefaultPort(Server server){
        return (80 != server.getPort());
    }
    public String toString(){
        return this.string;
    }
}
